﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.Business.Store {
    public class ProductOutStoreOrderProcessor : BaseProcessor {

        private int _pageSize;
        private ProductOutStoreOrderRepository _productOutStoreOrderRepository;

        public ProductOutStoreOrderProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._productOutStoreOrderRepository = new ProductOutStoreOrderRepository(pageSize);
        }

        public ProductOutStoreOrder FetchProductOutStoreOrderById(int productOutStoreOrderId) {
            try {
                return this._productOutStoreOrderRepository.FetchProductOutStoreOrderById(productOutStoreOrderId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定成品出库单失败！");
            }
        }

        public List<ProductOutStoreOrderDetailsViewModel> FetchProductOutStoreOrderDetailsViewModelList(int productOutStoreOrderId) {
            try {
                return this._productOutStoreOrderRepository.FetchProductOutStoreOrderByIdForView(productOutStoreOrderId).ProductOutStoreOrderDetails.Select(x => {
                    return new ProductOutStoreOrderDetailsViewModel() {
                        Key = x.ProductOutStoreOrderDetailsId,
                        ProductCode = x.Product.Code,
                        Quantity = x.Quantity,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime
                        //,ModifiedBy = x.ModifiedBy,
                        //ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定成品出库单失败！");
            }
        }

        public List<ProductOutStoreOrderViewModel> FetchProductOutStoreOrderViewModelList(ProductOutStoreOrderSearchModel searchModel, int pageIndex = 1) {
            try {
                List<ProductOutStoreOrder> resultList = this._productOutStoreOrderRepository.FetchProductOutStoreOrderList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new ProductOutStoreOrderViewModel() {
                        Key = x.ProductOutStoreOrderId,
                        Code = x.Code,
                        ProductOutStoreType = x.Basic.Code,
                        OrderStatus = MethodHelper.GetEnumsDescription(typeof(AuditStatus), x.Status),
                        AuditedBy = x.AuditedBy,
                        AuditedDatetime = x.AuditedDatetime,
                        Remarks = x.Remarks,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime
                        //,ModifiedBy = x.ModifiedBy,
                        //ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取成品出库单列表失败！");
            }
        }

        public void Insert(ProductOutStoreOrder productOutStoreOrder) {
            try {
                productOutStoreOrder.ProductOutStoreOrderId = Guid.NewGuid().GetHashCode();
                productOutStoreOrder.CreatedDatetime = DateTime.Now;
                productOutStoreOrder.Status = (int)AuditStatus.Waiting;
                productOutStoreOrder.ProductOutStoreOrderDetails.ToList().ForEach(x => {
                    x.CreatedBy = productOutStoreOrder.CreatedBy;
                    x.CreatedDatetime = productOutStoreOrder.CreatedDatetime;
                    x.ProductOutStoreOrderDetailsId = Guid.NewGuid().GetHashCode();
                });
                this._productOutStoreOrderRepository.Insert(productOutStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新成品出库单失败！");
            }
        }

        public void Update(ProductOutStoreOrder productOutStoreOrder) {
            try {
                ProductOutStoreOrder oldProductOutStoreOrder = this.FetchProductOutStoreOrderById(productOutStoreOrder.ProductOutStoreOrderId);
                if (!oldProductOutStoreOrder.RowVersion.SequenceEqual(productOutStoreOrder.RowVersion)) {
                    throw this.BuildException("更新成品出库单失败，数据不一致性错误！");
                }
                productOutStoreOrder.ModifiedDatetime = DateTime.Now;
                this._productOutStoreOrderRepository.UpdateProductOutStoreOrder(productOutStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新成品出库单失败！");
            }
        }

        public void Delete(ProductOutStoreOrder productOutStoreOrder) {
            try {
                this._productOutStoreOrderRepository.Delete(productOutStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除成品出库单失败！");
            }
        }

        public void Audit(ProductOutStoreOrder productOutStoreOrder) {
            try {
                ProductOutStoreOrder oldProductOutStoreOrder = this.FetchProductOutStoreOrderById(productOutStoreOrder.ProductOutStoreOrderId);
                if (!oldProductOutStoreOrder.RowVersion.SequenceEqual(productOutStoreOrder.RowVersion)) {
                    throw this.BuildException("审核成品出库单失败，数据不一致性错误！");
                }
                productOutStoreOrder.AuditedDatetime = DateTime.Now;
                this._productOutStoreOrderRepository.AuditProductOutStoreOrder(productOutStoreOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("审核成品出库单失败！");
            }
        }

        public void Delete(params int[] productOutStoreOrderIds) {
            try {
                this._productOutStoreOrderRepository.DeleteMultipleProductOutStoreOrder(productOutStoreOrderIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除成品出库单失败！");
            }
        }

        public int GetTheMaxFlowNumber(string startString) {
            try {
                return this._productOutStoreOrderRepository.GetTheMaxFlowNumber(startString) + 1;
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取成品出库单最大流水号失败！");
            }
        }

        public decimal GetTheCurrentProductStoreTotal(int productId) {
            try {
                return new ProductStoreRepository().FetchProductStoreByProductId(productId).Sum(x => x.Quantity);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取成品当前库存失败！");
            }
        }
    }
}
